;(function () {
	let payPlan = {
		template: `
			<div class="new-pay-plan-container" ref="vs">
				<div class="clearfix mb-10" v-if="otDisplay">
					<div class="pull-left">
						<h3 class="font-16">{{ title }}</h3>
						<slot name="title"></slot>
					</div>
					<div class="pull-right" v-if="!isEdit">
						<slot></slot>
					</div>
					<div class="pull-right" v-if="isEdit">
						<hl-button type="default" v-if="editDisplay" @on-click="isShowDialog = !isShowDialog">零头处理</hl-button>
						<hl-button type="default" @on-click="editAndSave">{{ editDisplay ? '保存' : '编辑' }}</hl-button>
					</div>
				</div>
				<div class="clearfix" v-if="!otDisplay">
					<slot name="titleOs"></slot>
				</div>
				<div class="pay-plan-table--edit" ref="payPlanTableScroll">
					<vue-scroll :ops="ops" ref="scrollTo" @handle-scroll="handleComplete">
						<div class="pay-table-header color-gray" v-for="(item, index) in newData" v-if="index == 0" :style="{width: width}">
							<div class="pay-table-td">账单开始日期</div>
							<div class="pay-table-td">账单结束日期</div>
							<div class="pay-table-td">收款日</div>
							<div class="pay-table-td" v-for="(item0, index0) in types" v-if="item[item0 + 'len']" :style="{width: item[item0 + 'len'] * payPlanTableWidth + 'px'}">
								<div class="pay-table-td-expense" v-for="(item1, index1) in item[item0]" :style="{width: payPlanTableWidth + 'px'}">{{ item1.name }}(元)</div>
							</div>
							<div class="pay-table-td" :style="{width: '280px', minWidth: '280px'}" v-if="oFlag">一次性费用(元)</div>
							<div class="pay-table-td" v-if="calcitent">意向金抵扣(元)</div>
							<div class="pay-table-td">合计</div>
						</div>
						<div class="pay-table-content" v-for="(item, index) in newData" :style="{width: width}" v-if="item.expenseList[0].expenseType != 'D' && item.expenseList[0].expenseType != 'O'">
							<div class="pay-table-td">
								<span>{{ item.beginDate }}</span>
							</div>
							<div class="pay-table-td">
									<span v-show="item.isUpdateFlag == 'N'">{{ item.endDate }}</span>
									<span v-if="!editDisplay" v-show="item.isUpdateFlag == 'Y'">{{ item.endDate }}</span>
									<web-calendar v-model="item.endDate" position="fixed"  @change="editBillEndDate(arguments, index)" v-if="editDisplay" v-show="item.isUpdateFlag == 'Y'"></web-calendar>
							</div>
							<div class="pay-table-td">
								<span v-if="!editDisplay">{{ item.paymentDeadLineDate }}</span>
								<web-calendar v-model="item.paymentDeadLineDate" position="fixed" v-if="editDisplay" @change="exportData(index)"></web-calendar>
							</div>
							<div class="pay-table-td" v-for="(item0, index0) in types" v-if="item[item0 + 'len']" :style="{width: item[item0 + 'len'] * payPlanTableWidth + 'px'}">
								<div class="pay-table-td-expense" v-for="(item1, index1) in item[item0]" :style="{height: index == 0 && oFlag && !alertFlag ? item['O'].length * 45 + 'px' : '45px', lineHeight: index == 0 && oFlag && !alertFlag ? item['O'].length * 45 + 'px' : '45px', width: payPlanTableWidth + 'px'}">
									<span v-if="!editDisplay || item1.expense == '#' || item1.expense == '实计'">{{ item1.expense | formatNum }}</span>
									<input @blur="totalCalculation" class="pay-plan-ipt" v-if="editDisplay && item1.expense != '#' && item1.expense != '实计'" type="number" v-model="item1.expense" />
								</div>
							</div>
							<div class="pay-table-td pay-table-td-expense-o" :style="{width: '280px', minWidth: '280px'}" v-if="oFlag">
								<div class="pay-table-td-expense clearfix" v-if="(index == 0 && !alertFlag) || item.alertFlag" :class="{'border-t': index1 > 0}" v-for="(item1, index1) in item['O']" :style="{width: '280px'}">
									<div class="border-r pull-left">{{ item1.name}}</div>
									<div class="pull-left border-r">
										<span v-if="!editDisplay || item1.expense == '#'">{{ item1.expense | formatNum }}</span>
										<input @blur="totalCalculation" class="pay-plan-ipt" v-if="editDisplay && item1.expense != '#'" type="number" v-model="item1.expense" />
									</div>
								</div>
								<span v-if="index > 0 && !item.alertFlag || (index == 0 && alertFlag)">--</span>
							</div>
							<div class="pay-table-td" v-if="calcitent">
								<span v-if="!editDisplay">{{ Number(item.intentionAmount) || '--' | formatNum }}</span>
								<input class="pay-plan-ipt" @blur="intentionAmountCarry(index)" v-if="editDisplay" type="number" v-model="item.intentionAmount" />
							</div>
							<div class="pay-table-td">{{ item.total | formatNum }}</div>
						</div>
					</vue-scroll>
				</div>

				<div class="mt-15 pay-subtotal-container" v-if="dData.length">
					<div class="pay-subtotal-table">
						<div class="color-gray pay-subtotal-table-td" style="width:140px;">账单开始日期</div>
						<div class="color-gray pay-subtotal-table-td" style="width:140px;">账单结束日期</div>
						<div class="color-gray pay-subtotal-table-td" style="width:140px;">收款日</div>
						<div class="dy-fx-1 color-gray pay-subtotal-table-td">费项名</div>
						<div class="dy-fx-1 color-gray pay-subtotal-table-td">金额</div>
						<div class="dy-fx-1 color-gray pay-subtotal-table-td" v-if="hejitotal">意向抵扣金</div>
					</div>
					<div class="pay-subtotal-table border-t" v-for="item in dData">
						<div class="pay-subtotal-table-td" style="width:140px;">{{item.beginDate}}</div>
						<div class="pay-subtotal-table-td" style="width:140px;">
							<span v-if="!editDisplay">{{ item.endDate }}</span>
							<web-calendar v-model="item.endDate" position="fixed" v-if="editDisplay" @change="exportData"></web-calendar>
						</div>
						<div class="pay-subtotal-table-td" style="width:140px;">
						<span v-if="!editDisplay">{{ item.paymentDeadLineDate }}</span>
							<web-calendar v-model="item.paymentDeadLineDate" position="fixed" v-if="editDisplay" @change="exportData"></web-calendar>
						</div>
						<div class="dy-fx-1 pay-subtotal-table-td">
							<div :class="{'border-t': index1 > 0}" v-for="(item1, index1) in item.expenseList">
								{{item1.name}}
							</div>
						</div>
						<div class="dy-fx-1 pay-subtotal-table-td">
							<div :class="{'border-t': index1 > 0}" v-for="(item1, index1) in item.expenseList">
								{{item1.expense | formatNum}}
							</div>
						</div>
						<div class="dy-fx-1 pay-subtotal-table-td" v-if="hejitotal">-{{hejitotal | formatNum}}</div>
					</div>
				</div>

				<div class="pay-subtotal mt-15" v-show="newData.length && subDisplay">
					<h3 class="font-16">小计：</h3>
					<div class="pay-subtotal-container" ref="paySubtotalScroll">
						<vue-scroll :ops="ops1" ref="scrollSubtotal" @handle-scroll="handleScroll">
							<div class="pay-subtotal-table clearfix">
								<div class="pay-subtotal-table-td" style="width: 140px;">
									<div>费用</div>
									<div class="border-t">金额（元）</div>
								</div>
								<div class="pay-subtotal-table-td clearfix" :style="{width: subtotalTableWidth * subtotalArr[item + 'len'] + 'px'}"  v-for="(item, index) in types" v-if="item != 'CM' && subtotalArr[item + 'len']">
									<div class="pull-left" :style="{width: subtotalTableWidth-1 + 'px'}" v-for="(item0, index0) in subtotalArr[item]">
										<div :class="{'border-r': index0 < subtotalArr[item].length-1}">{{ item0.name }}</div>
										<div :class="{'border-r': index0 < subtotalArr[item].length-1}" class="border-t">{{ item0.expense | formatNum }}</div>
									</div>
								</div>
								<div class="pay-subtotal-table-td" :style="{width: subtotalTableWidth + 'px'}" v-if="subtotalArr['Olen'] || dData.length">
									<div>{{ oObj.name }}</div>
									<div class="border-t">{{ oObj.expense | formatNum }}</div>
								</div>
								<div class="pay-subtotal-table-td" :style="{width: subtotalTableWidth + 'px'}" v-if="subtotalArr['CMlen']">
									<div>营业额抽成</div>
									<div class="border-t">实计</div>
								</div>
								<div class="pay-subtotal-table-td" :style="{width: subtotalTableWidth + 'px'}" v-if="intentionAmount">
									<div>意向金抵扣</div>
									<div class="border-t">{{ intentionAmount | formatNum }}</div>
								</div>
							</div>
						</vue-scroll>
					</div>
				</div>

				<div class="pay-plan-total" v-show="newData.length && totalDisplay">
					<strong>合计应付：</strong>
					<span class="pay-plan-total-rmb">{{ total | formatNum }}</span>
					<span>元</span>
					<span class="pay-plan-red" v-if="subtotalArr['CMlen']">(营业额抽成或与租金取高以实计)</span>
				</div>
				<hl-dialog title="数据精确" :visible="isShowDialog" @on-close="isShowDialog = !isShowDialog">
					<div style="padding: 20px 0 30px;">
						<hl-row class="mb-10 clearfix" style="overflow: initial;">
							<hl-col :span="3" class="line-height-34">数据精确：</hl-col>
							<hl-col :span="14">
								<hl-select :data="accuracyList" v-model="accuracyActive"  @on-change="handleChange" type="text" width="320" dropdownHeight="110"></hl-select>
							</hl-col>
						</hl-row>
						<hl-row class="mb-10">
							<hl-col :span="3" ><span style="color:#f9f9f9">--</span></hl-col>
							<hl-col :span="14">
								<span class="color-gray font-12">说明：1精确到1元，10精确到10元，以此类推。</span>
							</hl-col>
						</hl-row>
						<hl-row class="mb-10">
							<hl-col :span="3" ><span>处理方式：</span></hl-col>
							<hl-col :span="14">
								<hl-radio :data="processList" v-model="processActive" @on-click="handleClick"></hl-radio>
							</hl-col>
						</hl-row>
					</div>
					<div slot="footer">
							<hl-button type="primary" @on-click="amountReset">确认</hl-button>
							<hl-button type="outline" @on-click="isShowDialog = !isShowDialog">取消</hl-button>
					</div>
				</hl-dialog>
			</div>
		`,
		model: {
			prop: 'resulte',
			event:'on-resulte'
		},
		props: {
			alertFlag: {
				type: Boolean,
				default: false
			},
			isYajin: {
				type: Boolean,
				default: false
			},
			data: {
				type: Array,
				default: () => {return []}
			},
			eData: {
				type: Array,
				default: () => {return {}}
			},
			oTypes: {
				type: Array,
				default: () => {return ['O', 'T','AG']}
			},
			types: {
				type: Array,
				default: () => {return ['R', 'P', 'CM']}
			},
			intentionAmount: {
				type: [Number, String],
				default: 0
			},
			isEdit: {
				type: Boolean,
				default: true
			},
			otDisplay: {
				type: Boolean,
				default: true
			},
			title: {
				type: String,
				default: '付款计划'
			},
			subDisplay: {
				type: Boolean,
				default: true
			},
			totalDisplay: {
				type: Boolean,
				default: true
			}
		},
		filters: {
			formatNum:function(item){
				if (item == '实计') return '实计'
				if (isNaN(item)) return '0.00'
			  return hlNumberUtil.formatNum(item)
			},
		},
		watch: {
			data (val) {

				this.newData = JSON.parse(JSON.stringify([]))
				val.map(item => {
					if (item.expenseList && item.expenseList.length) {
						this.newData.push(item)
					}
				})

				this.editDisplay = false;
				setTimeout(() => {
					this.payPlanDataRecombination()
				}, 100)
			},
			eData (obj) {
				this.newData = JSON.parse(JSON.stringify([]))
				obj.map(item => {
					if (item.expenseList && item.expenseList.length) {
						this.newData.push(item)
					}
				})


				setTimeout(() => {
					this.payPlanDataRecombination()
				}, 30)
			}
		},
		data () {
			return {
				newData: [],
				dData: [],
				editDisplay: false,
				oFlag: false,
				width: '100%',
				subtotalTableWidth: 170,
				payPlanTableWidth: 140,
				subtotalArr: {},
				oObj: {name: '一次性费用', expense: 0},
				//所删除数据的索引
				delIndex: null,
				dikoujin: 0,
				hejitotal: 0,
				calcitent: 0,
				//总计应付
				total: 0,
				//
				isShowDialog: false,
				//精确度
				accuracyList: [
					{name: '0.1', code: '0.1'},
					{name: '1', code: '1'},
					{name: '10', code: '10'},
					{name: '100', code: '100'},
					{name: '1000', code: '1000'},
					{name: '10000', code: '10000'},
				],
				accuracyActive: [],
				//处理方式
				processList: [
					{name: '四舍五入', code: 'rounding'},
					{name: '向下取整', code: 'downward'},
					{name: '向上取整', code: 'ceil'},
				],
				processActive: ['rounding'],
				//滚动条配置
				scrollDisx: 0,
				scrollSubtotalX: 0,
				ops: {
				  bar: {
				    background: '#000',
						onlyShowBarOnScroll: false,
						keepShow: false,
						hoverStyle: false,
						size: '10px',
						opacity: 0.3
				  },
					scrollPanel: {
						scrollingX: true,
						scrollingY: false
					}
				},
				ops1: {
				  bar: {
				    background: '#000',
						onlyShowBarOnScroll: false,
						keepShow: false,
						hoverStyle: false,
						size: '10px',
						opacity: 0.3
				  },
					scrollPanel: {
						scrollingX: true,
						scrollingY: false
					}
				}
			}
		},
		methods: {
			handleComplete(vertical, horizontal) {
					this.scrollDisx = horizontal.process*100
			},
			handleScroll(vertical, horizontal) {
					this.scrollSubtotalX = horizontal.process*100
			},
			//付款计划数据重组
			payPlanDataRecombination () {
				if (!this.newData.length) {return};
				let obj = {O: []};
				this.dData = JSON.parse(JSON.stringify([]))
				let count = 4;
				this.calcitent = 0;
				this.dikoujin = 0;
				let width = 140;
				let oWidth = 0;
				this.types.map(item => {
					obj[item] = JSON.parse(JSON.stringify([]))
				})
				this.newData.map(item => {
					item.total = 0;
					item.intentionAmount = '0.00';
					item.beginDate = typeof item.beginDate == 'number' ? hlDateUtil.formatUTCDate(item.beginDate, 'yyyy-MM-dd') : item.beginDate
					item.endDate = typeof item.endDate == 'number' ? hlDateUtil.formatUTCDate(item.endDate, 'yyyy-MM-dd') : item.endDate
					item.paymentDeadLineDate = typeof item.paymentDeadLineDate == 'number' ? hlDateUtil.formatUTCDate(item.paymentDeadLineDate, 'yyyy-MM-dd') : item.paymentDeadLineDate
					this.types.map(item0 => {
						item[item0] = []
					})
					item['O'] = JSON.parse(JSON.stringify([]))
				})
				this.newData.map(item => {
					if (item.expenseList && item.expenseList.length) {
						item.expenseList.map(item0 => {
							if (item0.customPeriod) {
								item0.customPeriod.map(item1 => {
									item1.begin_date = hlDateUtil.formatUTCDate(item1.beginDate, 'yyyy-MM-dd')
									item1.end_date = hlDateUtil.formatUTCDate(item1.endDate, 'yyyy-MM-dd')
								})
							}
						})
					}
				})
				//操蛋的意向金特殊处理
				if (!this.isYajin) {
					if (this.intentionAmount) {
						count = 5;
					}
					if (this.intentionAmount) {
						this.calcitent = -JSON.parse(JSON.stringify(Math.abs(this.intentionAmount)))
						this.newData[0].intentionAmount = -JSON.parse(JSON.stringify(Math.abs(this.intentionAmount)))
					}
				}

				//根据expenseType分类
				this.newData.map((item, index2) => {
					if (item.expenseList && item.expenseList.length) {
						item.expenseList.map(exp => {
							// 除了意向金,一次性费用全部装在一起，顺序根据types里面数组顺序执行
							if (this.types.indexOf(exp.expenseType) >= 0) {
								item[this.types[this.types.indexOf(exp.expenseType)]].push(JSON.parse(JSON.stringify(exp)))
							}
							//一次性费用单独装在一个盒子里面，根据otypes来区分是否是一次性费项
							if (this.oTypes.indexOf(exp.expenseType) >= 0 && exp.isSameFirstRentFlag == 'Y') {
								item['O'].push(JSON.parse(JSON.stringify(exp)))
							}
							// 如果一次性费项目不是同周期费项，则账单类型为 D和 O 的单独拆出来 ，否则只提取出 O 的
							if (['D'].indexOf(exp.expenseType) >= 0) {
								this.dData.push({
									beginDate: item.beginDate,
									endDate: item.endDate,
									paymentDeadLineDate: item.paymentDeadLineDate,
									isUpdateFlag: item.isUpdateFlag,
									expenseList: [exp]
								})
							}
							if (['O'].indexOf(exp.expenseType) >= 0 && exp.isSameFirstRentFlag == 'N') {
								this.dData.push({
									beginDate: item.beginDate,
									endDate: item.endDate,
									paymentDeadLineDate: item.paymentDeadLineDate,
									isUpdateFlag: item.isUpdateFlag,
									expenseList: [exp],
									isSameFirstRentFlag: item.isSameFirstRentFlag
								})
							}

						})
					}
				})

				//操蛋的意向金特殊处理
				if (this.isYajin) {
					this.dikoujin = JSON.parse(JSON.stringify(Math.abs(this.intentionAmount)))
					let heji = 0;
					if (this.intentionAmount) {
						this.dData.map(item => {
							item.expenseList.map(item => {
								heji += Number(item.expense)
							})
						})

						if (heji < this.dikoujin) {
							this.newData[0].intentionAmount = -(Number(this.dikoujin)-heji)
							this.calcitent = -(Number(this.dikoujin)-heji)
							this.hejitotal = heji
						} else {
							this.hejitotal = this.dikoujin
						}

					}
					if (this.intentionAmount && heji < this.dikoujin) {
						count = 5;
					}
				}


				//等值赋予array
				this.newData.map(item => {
					for (let key in obj) {
						obj[key] = JSON.parse(JSON.stringify(obj[key].concat(item[key])))
					}
				})
				// 去除重复费用
				for (let key in obj) {
					obj[key] = hlArrayUtil.removeRepeat(obj[key], 'name')
				}
				//费项全部执行#处理，并且赋予索引
				for (let key in obj) {
					obj[key].map((item, index) => {
						item['expense'] = '#';
						item['expenselateFee'] = '#';
						item['customPeriod'] = [];
						item['index'] = index;
					})
				}
				// 类型相等赋予newdata
				this.newData.map(item => {
					for (let key in obj) {
						Object.keys(item).forEach(v=>{
								if (v == key) {
									item[key] = JSON.parse(JSON.stringify(item[key].concat(obj[key])))
								}
						})
					}
				})
				// 把索引给与已有费项
				this.newData.map(item => {
					for (let key in obj) {
						item[key].map(item0 => {
							Object.keys(item).forEach(v=>{
									if (v == key) {
										obj[key].map(item1 => {
											if (item0.name == item1.name) {
												item0['index'] = JSON.parse(JSON.stringify(item1['index']))
											}
										})
									}
							})
						})
					}
				})
				// 去除多余费项
				this.newData.map(item => {
					this.types.map(item0 => {
						item[item0] = hlArrayUtil.removeRepeat(item[item0], 'name')
					})
					item['O'] = hlArrayUtil.removeRepeat(item['O'], 'name')
				})

				// 金额俩位小数处理
				this.newData.map(item => {
					this.types.map(item0 => {
						item[item0].map(item1 => {
							if (!isNaN(item1.expense)) {
								item1.expense = Number(item1.expense).toFixed(2)
							}
						})
					})
					item['O'].map(item1 => {
						if (!isNaN(item1.expense)) {
							item1.expense = Number(item1.expense).toFixed(2)
						}
					})
				})
				// 根据索引排序
				this.newData.map(item => {
					this.types.map(item0 => {
						item[item0] = item[item0].sort(hlArrayUtil.compareAvg('index'))
						item[item0 + 'len'] = item[item0].length
					})
					item['O'] = item['O'].sort(hlArrayUtil.compareAvg('index'))
				})
				//操蛋的抽成金额单独处理
				this.newData.map(item => {
					item['CM'].map(item0 => {
						if (item0.expense == 0) {
							item0.expense = '实计'
						}
					})
				})

				// 导出数据处理函数
				this.exportData()
				// 每行总计处理函数
				this.totalCalculation()

				if (this.newData[0]['O'].length) {
					this.oFlag = true;
				}
				if (!this.newData[0]['O'].length) {
					this.oFlag = false;
				}
				// 宽度计算↓
				for (let key in obj) {
					if (key !== 'O') {
						count += obj[key].length
					}
				}
				//减去固定4项后，如果费项小于3则扩大宽度，原设定140px,一次性费项280px
				if (count - 4 < 3) {
					if (!this.newData[0]['O'].length) {
						this.payPlanTableWidth = (this.$refs.payPlanTableScroll.offsetWidth-140*4)/(count-4)
					}
					if (this.newData[0]['O'].length) {
						let w = (this.$refs.payPlanTableScroll.offsetWidth-140*4-280)/(count-4)
						this.payPlanTableWidth = w < 140 ? 140 : w
					}
				}

				// 滚动初始化
				this.rollInit()
			},
			//每行金额总计
			totalCalculation () {
				this.newData.map(item => {
					item.total = 0;
					this.types.map(item0 => {
						item[item0].map(item1 => {
							if (!isNaN(item1.expense)) {
								item.total += Number(item1.expense)
							}
						})
					})
					item['O'].map(item1 => {
						if (!isNaN(item1.expense)) {
							item.total += Number(item1.expense)
						}
					})
					item.total += Number(item.intentionAmount)
				})
				this.newData.map(item => {
					this.types.map(item0 => {
						item[item0].map(item1 => {
							if (item1.expense == '实计') {
								item.total = '实计'
							}
						})
					})
				})
				//页面显示更新
				this.$forceUpdate()
				//导出数据
				this.exportData()
				//小计
				this.subtotal()
			},
			//小计
			subtotal () {
				let obj = {O: []};

				let count = 0;
				if (this.intentionAmount) {
					count = 1
				}
				this.oObj.expense = 0;
				this.types.map(item => {
					obj[item] = []
				})
				for (let key in obj) {
					obj[key] = JSON.parse(JSON.stringify(this.newData[0][key]))
					let item = key + 'len'
					obj[item] = this.newData[0][item]
				}
				for (let key in obj) {
					if (Array.isArray(obj[key])) {
						obj[key].map(item => {
							if (item.expenseType != 'CM') {
								item.expense = 0
							}
							if (item.expenseType == 'CM') {
								item.expense = '实计'
							}
						})
					}
				}
				for (let key in obj) {
					if (Array.isArray(obj[key])) {
						obj[key].map(item => {
							this.newData.map(item0 => {
								item0[key].map(item1 => {
									if (item.name == item1.name) {
										if (!isNaN(item1.expense)) {
											item.expense += Number(item1.expense)
										}
									}
								})
							})
						})
					}
				}
				obj['O'].map(item => {
					this.oObj.expense += Number(item.expense)
				})
				this.dData.map(item => {
					item.expenseList.map(item1 => {
						this.oObj.expense += Number(item1.expense)
					})
				})
				for (let key in obj) {
					if (this.types.indexOf(key) > -1) {
						count += obj[key].length
					}
				}
				if (obj['O'].length) {
					obj['Olen'] = JSON.parse(JSON.stringify(obj['O'].length))
				}
				if (obj['O'].length || this.dData.length) {

					count +=1
				}

				if (count < 5) {
					this.subtotalTableWidth = (this.$refs.paySubtotalScroll.offsetWidth-140)/count
				}

				this.subtotalArr = obj;

				this.$emit('export-data', this.subtotalArr)

				this.payTotal()
			},
			//合计应付
			payTotal () {
				this.total = 0;
				for (let key in this.subtotalArr) {
					if (Array.isArray(this.subtotalArr[key])) {
						this.subtotalArr[key].map(item => {
							if (!isNaN(item.expense)) {
								this.total += Number(item.expense)
							}
						})
					}
				}

				this.dData.map(item => {
					item.expenseList.map(item1 => {
						if (!isNaN(item1.expense)) {
							this.total += Number(item1.expense)
						}
					})
				})

				this.total += this.intentionAmount
			},
			//意向金计算
			intentionAmountCarry (i) {
				let amount = 0;
				this.newData[i].intentionAmount = Number(this.newData[i].intentionAmount).toFixed(2)
				this.newData.map((item, index) => {
					if (index > 0) {
						amount += Number(item.intentionAmount)
					}
				})
				if (isNaN(this.newData[i].intentionAmount) || !this.newData[i].intentionAmount) {
					this.newData[i].intentionAmount = '0.00'
					hl.toast('非数字')
					this.totalCalculation()
					return
				}
				if (Number(this.newData[i].intentionAmount) > 0) {
					hl.toast('不能是正数')
					this.newData[i].intentionAmount = '0.00'
					this.totalCalculation()
					return
				}
				if (Math.abs(amount) > Math.abs(this.intentionAmount)) {
					this.newData[i].intentionAmount = '0.00'
				} else {
					this.newData[0].intentionAmount = (Number(this.calcitent) - Number(amount)).toFixed(2)
				}
				this.totalCalculation()
			},
			handleChange (arr) {
				this.accuracyActive = [arr[0].code]
			},
			handleClick (arr) {
				this.processActive = [arr[0].code]
			},
			// 金额重新处理
			amountReset () {
				if (!this.accuracyActive.length) {
					hl.toast('请选择数据精确度')
					return
				}
				this.newData.map(item => {
					this.types.map(item0 => {
						item[item0].map(item1 => {
							if (!isNaN(item1.expense)) {
								item1.expense = accurateNumber({state: this.processActive[0], accurate: this.accuracyActive[0], number: JSON.parse(JSON.stringify(item1.expense))})
							}
						})
					})
					item['O'].map(item1 => {
						if (!isNaN(item1.expense)) {
							item1.expense = accurateNumber({state: this.processActive[0], accurate: this.accuracyActive[0], number: JSON.parse(JSON.stringify(item1.expense))})
						}
					})
				})
				this.isShowDialog = false;
				this.processActive = ['rounding']
				this.accuracyActive = []
				this.totalCalculation()
			},
			//导出数据层
			exportData (index) {

				console.log(index);
				if (index == 0) {
					let dtrue = this.newData[0].expenseList;
					let dflag = false;
					dtrue.map(item => {
						if (item.expenseType  == 'D' && item.name  == '押金') {
							dflag = true;
						}
					})
					if (dflag) {
						this.editBillEndDate([this.newData[0]['endDate'], this.newData[0]['endDate']], index)
						return
					}

				}

				let newData = []
				this.newData.map((item, index) => {
					newData.push({
						"begin_date": item.beginDate,
						"end_date": item.endDate,
						"payment_deadline_date": item.paymentDeadLineDate,
						"expense_list": []
					})
				})

				this.newData.map((item, index) => {
					this.types.map(item0 => {
						item[item0].map(item1 => {
							if (!isNaN(item1.expense)) {
								newData[index].expense_list.push({
									"expense_id": item1.expenseId ? item1.expenseId : '#',
									"expense_name": item1.name ? item1.name : '#',
									"name": item1.name ? item1.name : '#',
									"expense_type": item1.expenseType ? item1.expenseType : '#',
									"expense_flow_type": item1.expenseFlowType ? item1.expenseFlowType : '#',
									"expense_late_fee": item1.expenselateFee ? String(item1.expenselateFee) : '#',
									"custom_period": item1.customPeriod ? item1.customPeriod.length ? item1.customPeriod : '#' : '#',
									"units_code": item1.unitsCode ? item1.unitsCode : '#',
									"amount_reveice": item1.expense ? String(item1.expense) : '#',
									"tax_percent": item1.taxPercent ? String(item1.taxPercent) : '0',
									"amount_receive_original": item1.amountReceiveOriginal ? String(item1.amountReceiveOriginal) : '#',
								})
							}
							if (item1.expenseType == 'CM' && item1.expense == '实计') {
								newData[index].expense_list.push({
									"expense_id": item1.expenseId ? item1.expenseId : '#',
									"expense_name": item1.name ? item1.name : '#',
									"name": item1.name ? item1.name : '#',
									"expense_type": item1.expenseType ? item1.expenseType : '#',
									"expense_flow_type": item1.expenseFlowType ? item1.expenseFlowType : '#',
									"expense_late_fee": item1.expenselateFee ? String(item1.expenselateFee) : '#',
									"custom_period": item1.customPeriod ? item1.customPeriod.length ? item1.customPeriod : '#' : '#',
									"units_code": item1.unitsCode ? item1.unitsCode : '#',
									"amount_reveice": item1.expense ? String(item1.expense) : '#',
									"tax_percent": item1.taxPercent ? String(item1.taxPercent) : '0',
									"amount_receive_original": item1.amountReceiveOriginal ? String(item1.amountReceiveOriginal) : '#',
								})
							}
						})
					})

					item['O'].map(item1 => {
						if (!isNaN(item1.expense)) {
							newData[index].expense_list.push({
								"expense_id": item1.expenseId ? item1.expenseId : '#',
								"expense_name": item1.name ? item1.name : '#',
								"name": item1.name ? item1.name : '#',
								"expense_type": item1.expenseType ? item1.expenseType : '#',
								"expense_flow_type": item1.expenseFlowType ? item1.expenseFlowType : '#',
								"expense_late_fee": item1.expenselateFee ? String(item1.expenselateFee) : '#',
								"custom_period": item1.customPeriod ? item1.customPeriod.length ? item1.customPeriod : '#' : '#',
								"units_code": item1.unitsCode ? item1.unitsCode : '#',
								"amount_reveice": item1.expense ? String(item1.expense) : '#',
								"tax_percent": item1.taxPercent ? String(item1.taxPercent) : '0',
								"amount_receive_original": item1.amountReceiveOriginal ? String(item1.amountReceiveOriginal) : '0',
							})
						}
					})

					newData[index].expense_list.push({
						"expense_id": '#',
						"expense_name": '意向金抵扣',
						"name": '意向金抵扣',
						"expense_type": 'I',
						"expense_flow_type": '#',
						"expense_late_fee": '#',
						"custom_period": '#',
						"units_code": '#',
						"amount_reveice": item.intentionAmount ? String(Math.abs(item.intentionAmount)) : '0',
						"tax_percent": '0',
						"amount_receive_original": '0',
					})

				})
				let dData = []
				this.dData.map((item, index) => {
					dData.push({
						"begin_date": item.beginDate,
						"end_date": item.endDate,
						"payment_deadline_date": item.paymentDeadLineDate,
						"expense_list": []
					})
				})

				this.dData.map((item, index) => {
					item.expenseList.map(item1 => {
						dData[index].expense_list.push({
							"expense_id": item1.expenseId ? item1.expenseId : '#',
							"expense_name": item1.name ? item1.name : '#',
							"name": item1.name ? item1.name : '#',
							"expense_type": item1.expenseType ? item1.expenseType : '#',
							"expense_flow_type": item1.expenseFlowType ? item1.expenseFlowType : '#',
							"expense_late_fee": item1.expenselateFee ? String(item1.expenselateFee) : '#',
							"custom_period": item1.customPeriod ? item1.customPeriod.length ? item1.customPeriod : '#' : '#',
							"units_code": item1.unitsCode ? item1.unitsCode : '#',
							"amount_reveice": item1.expense ? String(item1.expense) : '#',
							"tax_percent": item1.taxPercent ? String(item1.taxPercent) : '0',
							"amount_receive_original": item1.amountReceiveOriginal ? String(item1.amountReceiveOriginal) : '0',
						})

						dData[index].expense_list.push({
							"expense_id": '#',
							"expense_name": '意向金抵扣',
							"name": '意向金抵扣',
							"expense_type": 'I',
							"expense_flow_type": '#',
							"expense_late_fee": '#',
							"custom_period": '#',
							"units_code": '#',
							"amount_reveice": this.hejitotal ? String(Math.abs(this.hejitotal)) : '0',
							"tax_percent": '0',
							"amount_receive_original": '0',
						})
					})
				})
				dData.map(item => {
					newData.push(item)
				})

				this.$emit('on-resulte', newData)
			},
			//编辑保存
			editAndSave () {
				this.editDisplay = !this.editDisplay
				this.$emit('on-eisv-click', this.editDisplay,this.newData)
			},
			//编辑账单结束日期
			editBillEndDate (args, i) {
				console.log(args)
				this.delIndex = i
				if (timestamp(this.newData[i].beginDate) > timestamp(args[0])) {
					hl.toast('账单结束日期不能小于账单开始日期')
					setTimeout(() => {
						this.newData[i].endDate = args[1]
					}, 500)
					return
				}
				//导出当条所有有金额数据（如果有营业额，则导出营业额数据，并且金额重置为0）
				let data = {
					"begin_date": this.newData[i].beginDate,
					"end_date": this.newData[i].endDate,
					"payment_dead_line_date": this.newData[i].paymentDeadLineDate,
					"expense_list": []
				}

				let not_update_info_list = [];
				this.newData.map((item, index) => {
					if (index < i) {
						not_update_info_list.push({
							"beginDate": item.beginDate,
							"endDate": item.endDate,
							"paymentDeadLineDate": item.paymentDeadLineDate,
							"isUpdateFlag": item.isUpdateFlag,
							"expenseList": [],
						})
					}
				})
				this.types.map(type => {
					this.newData.map((item, index) => {
						if (index < i) {
							item[type].map(item1 => {
								if (!isNaN(item1.expense)) {
									not_update_info_list[index].expenseList.push({
										"expenseId": item1.expenseId ? item1.expenseId : null,
										"expenseName": item1.name ? item1.name : null,
										"name": item1.name ? item1.name : null,
										"expenseType": item1.expenseType ? item1.expenseType : null,
										"expenseFlowType": item1.expenseFlowType ? item1.expenseFlowType : null,
										"expenselateFee": item1.expenselateFee ? String(item1.expenselateFee) : null,
										"customPeriod": item1.customPeriod ? item1.customPeriod.length ? item1.customPeriod : null : null,
										"unitsCode": item1.unitsCode ? item1.unitsCode : null,
										"amountReveice": item1.expense ? String(item1.expense) : null,
										"expense": item1.expense ? String(item1.expense) : null,
										"taxPercent": item1.taxPercent ? String(item1.taxPercent) :null,
										"amount_receive_original": item1.amountReceiveOriginal ? String(item1.amountReceiveOriginal) : '0',
									})
								}
								if (item1.expense == '实计') {
									not_update_info_list[index].expenseList.push({
										"expenseId": item1.expenseId ? item1.expenseId : null,
										"expenseName": item1.name ? item1.name : null,
										"name": item1.name ? item1.name : null,
										"expenseType": item1.expenseType ? item1.expenseType : null,
										"expenseFlowType": item1.expenseFlowType ? item1.expenseFlowType : null,
										"expenselateFee": item1.expenselateFee ? String(item1.expenselateFee) : null,
										"customPeriod": item1.customPeriod ? item1.customPeriod.length ? item1.customPeriod : null : null,
										"unitsCode": item1.unitsCode ? item1.unitsCode : null,
										"amountReveice": item1.expense ? String(item1.expense) : null,
										"expense": item1.expense ? String(item1.expense) : null,
										"taxPercent": item1.taxPercent ? String(item1.taxPercent) :null,
										"amount_receive_original": item1.amountReceiveOriginal ? String(item1.amountReceiveOriginal) : '0',
									})
								}
							})

						}
					})
				})

				this.newData.map((item, index) => {
					if (index < i &&  index == 0) {
						item['O'].map(item1 => {
							not_update_info_list[index].expenseList.push({
								"expenseId": item1.expenseId ? item1.expenseId : null,
								"expenseName": item1.name ? item1.name : null,
								"name": item1.name ? item1.name : null,
								"expenseType": item1.expenseType ? item1.expenseType : null,
								"expenseFlowType": item1.expenseFlowType ? item1.expenseFlowType : null,
								"expenselateFee": item1.expenselateFee ? String(item1.expenselateFee) : null,
								"customPeriod": item1.customPeriod ? item1.customPeriod.length ? item1.customPeriod : null : null,
								"unitsCode": item1.unitsCode ? item1.unitsCode : null,
								"amountReveice": item1.expense ? String(item1.expense) : null,
								"expense": item1.expense ? String(item1.expense) : null,
								"taxPercent": item1.taxPercent ? String(item1.taxPercent) :null,
								"amount_receive_original": item1.amountReceiveOriginal ? String(item1.amountReceiveOriginal) : '0',
								"isSameFirstRentFlag": item1.isSameFirstRentFlag || 'N'
							})
						})
					}
				})



				console.log(not_update_info_list)
				let flag = false;  //检测全局有没有自定义时间段
				let currFlag = false; //检测当前点击这条数据有没有自定义时间段
				let maxDateGettime = 0; //当前这条自定义时间最大时间
				this.types.map(item => {
					this.newData.map(item0 => {
						item0[item].map(item1 => {
							if (item1.customPeriod) {
								if (item1.customPeriod.length) {
									flag = true;
								}
							}
						})
					})
				})
				this.types.map(item => {
					this.newData[i][item].map(item1 => {
						if (item1.customPeriod) {
							if (item1.customPeriod.length) {
								currFlag = true;
								maxDateGettime = item1.customPeriod[item1.customPeriod.length-1].endDate
							}
						}

						if (!isNaN(item1.expense)) {
							data.expense_list.push({
								"expense_id": item1.expenseId ? item1.expenseId : '#',
								"expense_name": item1.name ? item1.name : '#',
								"name": item1.name ? item1.name : '#',
								"expense_type": item1.expenseType ? item1.expenseType : '#',
								"expense_flow_type": item1.expenseFlowType ? item1.expenseFlowType : '#',
								"expense_late_fee": item1.expenselateFee ? String(item1.expenselateFee) : '#',
								"custom_period": item1.customPeriod ? item1.customPeriod.length ? item1.customPeriod : '#' : '#',
								"units_code": item1.unitsCode ? item1.unitsCode : '#',
								"amount_reveice": item1.expense ? String(item1.expense) : '#',
								"expense": item1.expense ? String(item1.expense) : '#',
								"tax_percent": item1.taxPercent ? String(item1.taxPercent) : '#',
								"amount_receive_original": item1.amountReceiveOriginal ? String(item1.amountReceiveOriginal) : '0',
							})
						}

						if (item1.expense == '实计') {
							data.expense_list.push({
								"expense_id": item1.expenseId ? item1.expenseId : '#',
								"expense_name": item1.name ? item1.name : '#',
								"name": item1.name ? item1.name : '#',
								"expense_type": item1.expenseType ? item1.expenseType : '#',
								"expense_flow_type": item1.expenseFlowType ? item1.expenseFlowType : '#',
								"expense_late_fee": item1.expenselateFee ? String(item1.expenselateFee) : '#',
								"custom_period": item1.customPeriod ? item1.customPeriod.length ? item1.customPeriod : '#' : '#',
								"units_code": item1.unitsCode ? item1.unitsCode : '#',
								"amount_reveice":  '0',
								"expense": '0',
								"tax_percent": item1.taxPercent ? String(item1.taxPercent) : '#',
								"amount_receive_original": item1.amountReceiveOriginal ? String(item1.amountReceiveOriginal) : '0',
							})
						}
					})
				})

				this.newData[i]['O'].map(item1 => {
					if (!isNaN(item1.expense)) {
						data.expense_list.push({
							"expense_id": item1.expenseId ? item1.expenseId : '#',
							"expense_name": item1.name ? item1.name : '#',
							"name": item1.name ? item1.name : '#',
							"expense_type": item1.expenseType ? item1.expenseType : '#',
							"expense_flow_type": item1.expenseFlowType ? item1.expenseFlowType : '#',
							"expense_late_fee": item1.expenselateFee ? String(item1.expenselateFee) : '#',
							"custom_period": item1.customPeriod ? item1.customPeriod.length ? item1.customPeriod : '#' : '#',
							"units_code": item1.unitsCode ? item1.unitsCode : '#',
							"amount_reveice": item1.expense ? String(item1.expense) : '#',
							"expense": item1.expense ? String(item1.expense) : '#',
							"tax_percent": item1.taxPercent ? String(item1.taxPercent) : '#',
							"amount_receive_original": item1.amountReceiveOriginal ? String(item1.amountReceiveOriginal) : '0',
						})
					}
				})

				// this.newData = []

				//根据不同状态删除数据
				// if (!flag && !currFlag) {
				// 	console.log(1234)
				// 	let dateNow = JSON.parse(JSON.stringify(this.newData[i].beginDate))
				// 	console.log(12345)
				// 	let num = 0;
				// 	this.newData.splice(i, 1)
				// 	for (let j = 0; j < this.newData.length; j++) {
				// 		if (timestamp(dateNow) < timestamp(this.newData[j].beginDate)) {
				// 			num++
				// 		}
				// 	}
				// 	this.newData.splice(i, num)
				// }
				// if (flag) {
				// 	for (let j = 0; j < this.newData.length; j++) {
				// 		let item = this.newData[j].expenseList;
				// 		for (let k = 0; k < item.length; k++) {
				// 			if (j >= i && !item[k].customPeriod) {
				// 				this.newData.splice(j, 1)
				// 				j--
				// 			}
				// 			if (item[k].customPeriod) {
				// 				break
				// 			}
				// 		}
				// 	}
				// }
				// if (flag && currFlag) {
				// 	if (timestamp(this.newData[i].endDate) > maxDateGettime) {
				// 		hl.toast('账单结束日期不能大于周期性费项最晚日期')
				// 		setTimeout(() => {
				// 			this.newData[i].endDate = args[1]
				// 		}, 500)
				// 		return
				// 	}
				// 	this.newData.splice(i, 1)
				// }
				this.$emit('on-change-date', data, not_update_info_list)
			},
			//滚动初始化
			rollInit () {
				const self = this;
				let disx = 0;
				function scrollFunc (e, ref){
					e=e||window.event;
					if (e&&e.preventDefault){
						if (e.deltaY > 0) {
							disx += 10
						}
						if (e.deltaY < 0) {
							disx -= 10
						}
						if (disx > 100) {disx=100}
						if (disx < 0) {disx = 0}
						self.$refs[ref].scrollTo(
							{
								x: disx + '%'
							},
							10
						);
						e.preventDefault();
						e.stopPropagation();
					}else{
					 e.returnvalue=false;
					 return false;
					}
				}

				if (this.newData.length) {
					this.$refs.paySubtotalScroll.onmouseenter = function () {
						disx = self.scrollSubtotalX;
						scrollLeft(self.$refs.paySubtotalScroll, 'scrollSubtotal')
					}

					this.$refs.paySubtotalScroll.onmouseleave = function () {
						self.$refs.paySubtotalScroll.onmousewheel = null;
					};

					this.$refs.payPlanTableScroll.onmouseenter = function () {
						disx = self.scrollDisx;
						scrollLeft(self.$refs.payPlanTableScroll, 'scrollTo')
					}

					this.$refs.payPlanTableScroll.onmouseleave = function () {
						self.$refs.payPlanTableScroll.onmousewheel = null;
					};
				}

				function scrollLeft (el, ref) {
					function addEventListFn(e) {scrollFunc(e, ref)}
					el.onmousewheel = addEventListFn
				}
			}
		},
		mounted () {
			let style = document.createElement('style')
			style.innerHTML = `
				.__vuescroll .__panel{overflow:hidden !important}
			`
			this.$refs.vs.appendChild(style)
			this.newData = JSON.parse(JSON.stringify(this.data));

			if(this.data.length){
				setTimeout(() => this.payPlanDataRecombination() , 30 )
			}
		}
	}

	function accurateNumber (ops) {
	  var result;
	  if (!ops.number) return '0.00';
	  //四舍五入
	  if (ops.state == 'rounding') {
	    if (ops.accurate == '0.1') {
	      result = Number(ops.number).toFixed(1)+'0'
	    }
	    if (ops.accurate == '1') {
	      result = Number(ops.number).toFixed(0)+'.00'
	    }
	    if (ops.accurate == '10') {
	      ops.number = ops.number/10;
	      if (ops.number >= 0.5) {
	        result = Math.round(ops.number)+'0.00'
	      }
	      if (ops.number < 0.5) {
	        result = Math.round(ops.number)+'.00'
	      }
	    }
	    if (ops.accurate == '100') {
	      ops.number = ops.number/100;
	      if (ops.number >= 0.5) {
	        result = Math.round(ops.number)+'00.00'
	      }
	      if (ops.number < 0.5) {
	        result = Math.round(ops.number)+'0.00'
	      }
	    }
	    if (ops.accurate == '1000') {
	      ops.number = ops.number/1000;
	      if (ops.number >= 0.5) {
	        result = Math.round(ops.number)+'000.00'
	      }
	      if (ops.number < 0.5) {
	        result = Math.round(ops.number)+'00.00'
	      }
	    }
	    if (ops.accurate == '10000') {
	      ops.number = ops.number/10000;
	      if (ops.number >= 0.5) {
	        result = Math.round(ops.number)+'0000.00'
	      }
	      if (ops.number < 0.5) {
	        result = Math.round(ops.number)+'000.00'
	      }
	    }
	  }
	  //向下取整
	  if (ops.state == 'downward') {
	    if (ops.accurate == '0.1') {
	      if (ops.number>1) {
	        result = Math.floor(ops.number*10)/10+'0'
	      }
	      if (ops.number<1) {
	        result = Math.floor(ops.number*10)/10+'0'
	      }
	    }
	    if (ops.accurate == '1') {
	      if (ops.number>1) {
	        result = Math.floor(ops.number)+'.00'
	      }
	      if (ops.number<1) {
	        result = Math.floor(ops.number)+'.00'
	      }
	    }
	    if (ops.accurate == '10') {
	      ops.number = ops.number/10;
	      if (ops.number>1) {
	        result = Math.floor(ops.number)+'0.00'
	      }
	      if (ops.number<1) {
	        result = Math.floor(ops.number)+'.00'
	      }
	    }
	    if (ops.accurate == '100') {
	      ops.number = ops.number/100;
	      if (ops.number>1) {
	        result = Math.floor(ops.number)+'00.00'
	      }
	      if (ops.number<1) {
	        result = Math.floor(ops.number)+'0.00'
	      }
	    }
	    if (ops.accurate == '1000') {
	      ops.number = ops.number/1000;
	      if (ops.number>1) {
	        result = Math.floor(ops.number)+'000.00'
	      }
	      if (ops.number<1) {
	        result = Math.floor(ops.number)+'00.00'
	      }
	    }
	    if (ops.accurate == '10000') {
	      ops.number = ops.number/10000;
	      if (ops.number>1) {
	        result = Math.floor(ops.number)+'0000.00'
	      }
	      if (ops.number<1) {
	        result = Math.floor(ops.number)+'000.00'
	      }
	    }
	  }
	  // 向上取整
	  if (ops.state == 'ceil') {
	    if (ops.accurate == '0.1') {
	      if (ops.number>1) {
	        result = Math.ceil(ops.number*10)/10+'0'
	      }
	      if (ops.number<1) {
	        result = Math.ceil(ops.number*10)/10+'0'
	      }
	    }
	    if (ops.accurate == '1') {
	      if (ops.number>1) {
	        result = Math.ceil(ops.number)+'.00'
	      }
	      if (ops.number<1) {
	        result = Math.ceil(ops.number)+'.00'
	      }
	    }
	    if (ops.accurate == '10') {
	      ops.number = ops.number/10;
	      if (ops.number>1) {
	        result = Math.ceil(ops.number)+'0.00'
	      }
	      if (ops.number<1) {
	        result = Math.ceil(ops.number)+'.00'
	      }
	    }
	    if (ops.accurate == '100') {
	      ops.number = ops.number/100;
	      if (ops.number>1) {
	        result = Math.ceil(ops.number)+'00.00'
	      }
	      if (ops.number<1) {
	        result = Math.ceil(ops.number)+'0.00'
	      }
	    }
	    if (ops.accurate == '1000') {
	      ops.number = ops.number/1000;
	      if (ops.number>1) {
	        result = Math.ceil(ops.number)+'000.00'
	      }
	      if (ops.number<1) {
	        result = Math.ceil(ops.number)+'00.00'
	      }
	    }
	    if (ops.accurate == '10000') {
	      ops.number = ops.number/10000;
	      if (ops.number>1) {
	        result = Math.ceil(ops.number)+'0000.00'
	      }
	      if (ops.number<1) {
	        result = Math.ceil(ops.number)+'000.00'
	      }
	    }
	  }
	  return Number(result).toFixed(2);
	}

	function timestamp (time) {
		return new Date(time).getTime()
	}

	Vue.component('hl-pay-plan',payPlan)
} ())
